import user from '../../../utils/user.js'
import {
    createClientQrCode,
} from '../../../api/app.js'
import { saveQrcodeCache } from '../../api/insurance'
import {
    base64ToWxfile
} from '../../../utils/util.js'
const app = getApp()
Page({
    data: {
        bg: [{
            preview: '/packageMicroInsurance/assets/img/poster/poster_bg_1.png',
            original: '/packageMicroInsurance/assets/img/poster/poster_bg_1_1.png'
        },
        {
            preview: '/packageMicroInsurance/assets/img/poster/poster_bg_2.png',
            original: '/packageMicroInsurance/assets/img/poster/poster_bg_2_1.png'
        },
        {
            preview: '/packageMicroInsurance/assets/img/poster/poster_bg_3.png',
            original: '/packageMicroInsurance/assets/img/poster/poster_bg_3_1.png'
        },
        {
            preview: '/packageMicroInsurance/assets/img/poster/poster_bg_4.png',
            original: '/packageMicroInsurance/assets/img/poster/poster_bg_4_1.png'
        },
        {
            preview: '/packageMicroInsurance/assets/img/poster/poster_bg_5.png',
            original: '/packageMicroInsurance/assets/img/poster/poster_bg_5_1.png'
        }
        ],
        active: 1,
        postStatus: true,
        supplierName: '',
        supplierId: '',
        workers: [],
        showPosterStyle: false,
        record: true,
        type:'',
        coord:'',
        insuranceId:''
    },
    onReady() {
        user.waitLogin().then(() => {
            this.setData({
                name: user.userName,
                postStatus: true
            })
            this.saveQrCode()
        })

    },
    onLoad(option) {
        console.log(option,'option')
        this.setData({
            supplierName: option.projectName.split('有限公司')[0],
            insuranceId: option.projectId,
            coord: option.coord
        })
        this.setData({
            supplierNameEnglish: '',
            supplierId: option.projectId,
            isBook: !!option.book,
            record: !option.notRecord,
            active: option.poster || 1,
            type: option.type
        })
        this.createInviteQrCode()
        // 获取通过邀请码进入的列表
    },
    // onUnload(){
    //     console.log(this.data.socket)
    //     this.data.socket.close()
    // },
    createInviteQrCode() {
        saveQrcodeCache({
            key: 'insurance',
            projectId: this.data.insuranceId,
            data: 'INID=' + this.data.insuranceId + '&UID=' + user.userId + '&coord=' + this.data.coord
        }).then(res=>{
            let scene = ''
            scene = 'cacheId=' + res
            createClientQrCode({
                page: 'pages/join/join',
                scene: scene,
                width: 280
            }).then(res => {
                var qrCode = res // 后台返回的base64图片流
                base64ToWxfile(qrCode, Math.ceil(Math.random() * 10000000000) + '.png').then(path => {
                    this.setData({
                        qrcode: path
                    })
                    this.saveQrCode()
                })
            })
        })
    },
    selectBg(event) {
        console.log(event,'event')
        this.setData({
            active: event.currentTarget.dataset.key,
            postStatus: false
        })
        this.setData({
            postStatus: true
        })
        wx.showLoading({
            title: '正在生成请稍后'
        })
        setTimeout(() => {
            wx.hideLoading()
            this.saveQrCode()
        }, 100)
        // this.saveQrCode()
    },
    saveQrCode() {
        const ctx = wx.createCanvasContext('myCanvas')
        ctx.save()
        ctx.rect(0, 0, 320, 500)
        ctx.setFillStyle('#fff')
        ctx.fill()
        ctx.clip() // 剪切
        // 背景放置
        ctx.drawImage(this.data.bg[this.data.active].original, 0, 0, 320, 455, 0, 0)

        // 圆形
        ctx.beginPath()
        // this.data.supplierName.length > 12 ? 13 : 16 210
        ctx.arc(160, this.data.supplierName.length > 12 ? 210 : 200 , 80, 0, Math.PI * 2, false)
        ctx.closePath()
        ctx.fill()
        ctx.clip()

        // 二维码放置
        ctx.drawImage(this.data.qrcode, 90, this.data.supplierName.length > 12 ? 150 : 140, 140, 140, 0, 0)
        ctx.draw()
        ctx.restore()

        // 字体
        ctx.font =
		'normal normal 20px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
	
        // 邀请内容
        // ctx.setTextAlign("left")
        // ctx.setFillStyle('#fff')
        // ctx.fillText(this.data.supplierName, 22, 80)
        // ctx.fillText('邀请您加入', 30 + (user.userName.length * 22), 80)
        // ctx.font =
        // 	'normal normal 24px Monospaced Number,Chinese Quote,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,PingFang SC,Hiragino Sans GB,Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif'
        // ctx.fillText(this.data.supplierName.substr(0, 12), 22, 120)
        // ctx.fillText(this.data.supplierName.substr(12), 22, 154)

        ctx.textAlign = 'center'
        ctx.setFontSize(this.data.supplierName.length > 12 ? 13 : 16)
        ctx.setFillStyle('#9497B1')
        // 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 this.data.supplierName this.data.supplierName
        ctx.fillText(this.data.supplierName.slice(0,12), 160, 125)
        ctx.fillText(this.data.supplierName.slice(12), 160, 143)
        ctx.setFontSize(12)
        ctx.setFillStyle('#fff')
        // 公司简称
        if(user.companyAbbreviation){
            // 装饰品
            // 35 275 150 + (6*10)
            ctx.drawImage('/packageMicroInsurance/assets/img/poster/ornament-left.png', 155 - (user.companyAbbreviation.length * 13.5), 370, 8, 8, 0, 0)
            ctx.drawImage('/packageMicroInsurance/assets/img/poster/ornament-right.png', 155 + (user.companyAbbreviation.length * 13.5),370, 8, 8, 0, 0)
            ctx.setFontSize(22)
            ctx.setFillStyle('#fff')
            // this.data.supplierName
            ctx.fillText(user.companyAbbreviation, 160, 380)  
        }
        // 公司全称
        ctx.setFontSize(12)
        ctx.setFillStyle('#fff')
        ctx.fillText(user.companyName, 160, user.companyAbbreviation ? 400 : 38)


 

        ctx.draw(true)
        ctx.restore()
        // this.savecanvas()
    },
    savecanvas() {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 350,
            height: 500,
            canvasId: 'myCanvas',
            success: function (res) {
                let img = res.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: img,
                    success() {
                        app.toast('成功保存', 'success')
                    },
                    fail() {
                        app.toast('保存失败')
                    }
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    showMorePosterStyle(){
        this.setData({
            showPosterStyle: true
        })
    }
    // onShareAppMessage(event) {
    // 	console.log('分享', event)
    // 	return {
    // 		title: user.userName + '邀请您加入「' + user.companyName + '」',
    // 		path: '/page/index/index?shareUserId=' + user.userId,
    // 	}
    // }
})